package com.example.gulimall.ssoclient.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HelloController {

    @GetMapping("/hello")
    public String hello(){
        System.out.println("到这里来了");
        return "hello";
    }

    // 目标数据
    @GetMapping("/employees")
    public String employees(Model model, HttpSession httpSession,
                            @RequestParam(value = "token",required = false) String token,
                            @RequestParam(value = "sso_token",required = false) String sso_token){

        // System.out.println("此时token是"+token);
        // session或者token都可以
        Object loginUser = httpSession.getAttribute("loginUser");
        if(loginUser == null && StringUtils.isEmpty(token) && StringUtils.isEmpty(sso_token)){
            // 没登录 所以让他去登录 然后重定向地址用来最后跳回
            return "redirect:http://127.0.0.1:8080/login?redirect_url=http://localhost:8081/employees";
        }else{
            List<String> emps = new ArrayList<>();
            emps.add("张三");
            emps.add("李四");
            emps.add("王五");
            model.addAttribute("emps",emps);
            return "list";
        }
    }
}
